package ua.kpi.fpm.km61.strat.sp.gui;

import java.awt.Color;
import java.awt.Component;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
import java.util.Vector;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import tetrad.Tetrad;
import ua.kpi.fpm.km61.strat.sp.la.LexicalAnalisys;
import ua.kpi.fpm.km61.strat.sp.la.MarkedString;
import ua.kpi.fpm.km61.strat.sp.la.ResString;
import ua.kpi.fpm.km61.strat.sp.st.Block;
import ua.kpi.fpm.km61.strat.sp.st.Symbol;
import ua.kpi.fpm.km61.strat.sp.st.SymbolAnalizer;
import util.Word;

public class MainFrame extends javax.swing.JFrame {

    public MainFrame() {
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableLA = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextAreaGrammar = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTableTSA = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextAreaST = new javax.swing.JTextArea();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTableTI = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane7 = new javax.swing.JScrollPane();
        jTableTriads = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaCode = new javax.swing.JTextArea();
        jTextFieldCaretPosition = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTableLA.setModel(new LATableModel());
        jTableLA.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(jTableLA);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("LEXER", jPanel1);

        jTextAreaGrammar.setBackground(new java.awt.Color(233, 233, 233));
        jTextAreaGrammar.setColumns(20);
        jTextAreaGrammar.setEditable(false);
        jTextAreaGrammar.setFont(new java.awt.Font("Courier New", 0, 12));
        jTextAreaGrammar.setRows(5);
        jScrollPane3.setViewportView(jTextAreaGrammar);
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("rules.txt")));
            jTextAreaGrammar.read(isr, isr);
            jTextAreaGrammar.setCaretPosition(0);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.toString(), "Помилка", JOptionPane.ERROR_MESSAGE);
            dispose();
        }

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("GRAMMAR", jPanel2);

        jTableTSA.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTableTSA.setCellSelectionEnabled(true);
        jTableTSA.getTableHeader().setReorderingAllowed(false);
        jScrollPane5.setViewportView(jTableTSA);

//        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
//        jPanel4.setLayout(jPanel4Layout);
//        jPanel4Layout.setHorizontalGroup(
//            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
//                .addContainerGap()
//                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
//                .addContainerGap())
//        );
//        jPanel4Layout.setVerticalGroup(
//            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGroup(jPanel4Layout.createSequentialGroup()
//                .addContainerGap()
//                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
//                .addContainerGap())
//        );
//
//        jTabbedPane1.addTab("PARSING TABLE", jPanel4);

        jTextAreaST.setColumns(20);
        jTextAreaST.setFont(new java.awt.Font("Courier new", 0, 12));
        jTextAreaST.setRows(5);
        jScrollPane4.setViewportView(jTextAreaST);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("PARSER", jPanel5);

        jTableTI.getTableHeader().setReorderingAllowed(false);
        jScrollPane6.setViewportView(jTableTI);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("NAMING TABLE", jPanel3);

        jTableTriads.getTableHeader().setReorderingAllowed(false);
        jScrollPane7.setViewportView(jTableTriads);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("TETRADS", jPanel6);

        jTextAreaCode.setColumns(20);
        jTextAreaCode.setFont(new java.awt.Font("Lucida Console", 0, 14));
        jTextAreaCode.setRows(5);
        jTextAreaCode.setText(
        		"public class TestClass {\n" +
        		" public int f1;\n" +
        		" private int f2;\n\n" +
        		" public TestClass() {\n" +
        		"  f1 = 19+f2; \n" +
        		"  f2 = f1*13.56+2;\n" +
        		"  if(f1=f2){\n" +
        		"    f1=1;\n" +
        		"  }\n" +
        		" }\n\n" +
        		" public void method(int x){\n" +
        		"  TestClass cl = new TestClass();\n" +
        		"  int z=cl.f1*(cl.f2-1);\n" +
        		" }\n" +
        		"}\n");  
        jTextAreaCode.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTextAreaCodeCaretUpdate(evt);
            }
        });
        jScrollPane1.setViewportView(jTextAreaCode);

        jTextFieldCaretPosition.setEditable(false);

        jMenu1.setText("File");

        jMenuItem2.setText("Open");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);
        jMenu1.add(jSeparator1);

        jMenuItem3.setText("Exit");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Action");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        jMenuItem1.setText("GENERATE");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);


        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldCaretPosition, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldCaretPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        try {
			MainFrame.writeTextToFile("code.txt", jTextAreaCode.getText());
		} catch (IOException e) {
			e.printStackTrace();
		}
		File code = new File("code.txt");
        TreeSet<ResString> resultLA = doLA(code);
        jTextAreaST.setText("");
        jTableLA.setModel(new LATableModel());

        
        jTableLA.setModel(new LATableModel(resultLA));
        jTableLA.getColumnModel().getColumn(0).setPreferredWidth(25);
        jTableLA.getColumnModel().getColumn(1).setPreferredWidth(60);
        jTableLA.getColumnModel().getColumn(2).setPreferredWidth(200);
        jTableLA.getColumnModel().getColumn(3).setPreferredWidth(40);
        jTableLA.getColumnModel().getColumn(4).setPreferredWidth(40);
        jTableLA.getColumnModel().getColumn(0).setCellRenderer(new ColorColumnRenderer(Color.getHSBColor(0, 0, (float)0.93), Color.black));


//
//
//        Syntax syntax = new Syntax(container);
//        try {
//            syntax.buildTable();
//        } catch (Exception e) {
//            JOptionPane.showMessageDialog(this, e.getMessage(),"Помилка", JOptionPane.ERROR_MESSAGE);
//            return;
//        }
//        
//        jTableTSA.setModel(new MyParseTableModel(syntax.getParseTable(), syntax.getNonTerminals(), syntax.getTerminals()));
//        jTableTSA.getColumnModel().getColumn(0).setCellRenderer(new ColorColumnRenderer(Color.getHSBColor(0, 0, (float)0.93), Color.black));
//        jTableTSA.getColumnModel().getColumn(0).setPreferredWidth(170);
//
//        try {
//            syntax.buildSyntax();
//        } catch (Exception e) {
//            jTextAreaSintax.setText("Пор�?док розбору до зу�?трічі помилки:\n" + syntax.getRuleUsageLog());
//            JOptionPane.showMessageDialog(this, e.getMessage(),"Помилка", JOptionPane.ERROR_MESSAGE);
//            return;
//        }
//
//        jTextAreaSintax.setText(syntax.getRuleUsageLog());
//
        
//        SymbolTable symbolTable;// = null;
//        try {
//            symbolTable = new SymbolTable(container, syntax);
//        } catch (Exception e) {
//            JOptionPane.showMessageDialog(this, e.getMessage(),"Помилка", JOptionPane.ERROR_MESSAGE);
//            return;
//        }
        ArrayList<Block> data = new ArrayList<Block>();
        SymbolAnalizer syA = new SymbolAnalizer();
        try{
        	syA.analize(resultLA);
        	data.add(syA.getBase());
            syA.analizeSymbols(syA.getBase(), resultLA);
            jTextAreaST.setText(data.get(0).toString() +"\n" + syA.getBase().symbols() +"\n" + syA.getBase().find("zzz"));
        } catch(RuntimeException e)  {
        	JOptionPane.showMessageDialog(this, e.getMessage(),
					"SYMBOL ERROR", JOptionPane.ERROR_MESSAGE);
        }

//        jTableTI.getColumnModel().getColumn(2).setPreferredWidth(200);
//        jTableTI.getColumnModel().getColumn(3).setPreferredWidth(40);
//        jTableTI.getColumnModel().getColumn(4).setPreferredWidth(40);
        
        
//
//        Triads triads = new Triads(container, syntax, symbolTable);
//        try {
//            triads.buildTriads();
//        } catch (Exception e) {
//            JOptionPane.showMessageDialog(this, e.getMessage(),"Помилка", JOptionPane.ERROR_MESSAGE);
//            return;
//        }
//
//        //jTextAreaTriads.setText(triads.getAsString());
//        jTableTriads.setModel(new TetradsTableModel());
//        jTableTriads.getColumnModel().getColumn(0).setCellRenderer(new ColorColumnRenderer(Color.getHSBColor(0, 0, (float)0.93), Color.black));
//        jTableTriads.getColumnModel().getColumn(0).setPreferredWidth(10);
//        jTableTriads.getColumnModel().getColumn(1).setPreferredWidth(50);
//        jTableTriads.getColumnModel().getColumn(2).setPreferredWidth(80);
//        jTableTriads.getColumnModel().getColumn(3).setPreferredWidth(80);
//
//
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    static void writeTextToFile(String filename, String text) throws IOException{

        try {
           BufferedWriter out = new BufferedWriter(new FileWriter(filename));
           out.write(text);
           out.close();
       } catch (IOException e) {
    	   
       }

   }
    
	public TreeSet<ResString> doLA(File file) {
		TreeSet<ResString> resultLA = new TreeSet<ResString>();
		boolean b = false;
		LexicalAnalisys la = new LexicalAnalisys(file);
		List<MarkedString> l0 = la.delBlockComments();
		l0 = la.delBlockCommentsML(l0);
		List<MarkedString> l1 = la.delLineComments(l0);
		List<MarkedString> l2 = la.buildLecsem(l1);
		List<MarkedString> l3 = la.splitByRegexp(l2, LexicalAnalisys.DELIMRX,
				"del");
		l3 = la.splitByRegexp(l3, LexicalAnalisys.OPERATIONSRX, "opr");
		l3 = la.splitByIDent(l3, "ids");
		l3 = la.splitByRegexp(l3, LexicalAnalisys.litRXd, "dou");
		l3 = la.splitByRegexp(l3, LexicalAnalisys.litRXi, "int");
		l3 = la.splitByRegexp(l3, LexicalAnalisys.dotRX, "opr");
		for (ResString r : la.getReslist()) {
			resultLA.add(r);
		}
		int k = 0;
		while (k < l3.size()) {
			if (l3.get(k).getColnum() == 1 && l3.get(k).getStrnum() == 1
					|| l3.get(k).getStr().equals("")) {
				l3.remove(k);
			} else {
				if (l3.get(k).getStr().equals(".")) {
					ResString res = new ResString();
					res.setStr(l3.get(k).getStr());
					res.setStrnum(l3.get(k).getStrnum());
					res.setColnum(l3.get(k).getColnum());
					res.setLex("opr");
					resultLA.add(res);
					l3.remove(k);
				} else {
					k++;
				}
			}
		}

		if (l3.size() > 0) {
			StringBuilder errStr = new StringBuilder();
			for (int i = 0; i < l3.size(); i++) {
				errStr.append(l3.get(i).toString() + "\n");
			}
			JOptionPane.showMessageDialog(this, errStr.toString(),
					"LEXICAL ERRORS", JOptionPane.ERROR_MESSAGE);
			resultLA = new TreeSet<ResString>();
		}
		return resultLA;
	}
    
    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        JFileChooser op = new JFileChooser();
        op.addChoosableFileFilter(new myFilter());
        int result = 0;
        result = op.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            try {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(op.getSelectedFile()));
                jTextAreaCode.read(isr, isr);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(MainFrame.this,
                            "Cant load data!\n",
                            "File error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jTextAreaCodeCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextAreaCodeCaretUpdate
        int caretPosition = jTextAreaCode.getCaretPosition();
        int lastEnter = -1;
        int rows = 1;
        for (int i = 0; i<caretPosition; i++) {
            if (jTextAreaCode.getText().charAt(i) == '\n') {
                rows++;
                lastEnter = i;
            }
        }
        jTextFieldCaretPosition.setText(rows + ":" + (caretPosition - lastEnter-1));
    }//GEN-LAST:event_jTextAreaCodeCaretUpdate



    private class myFilter extends javax.swing.filechooser.FileFilter {

        @Override
        public boolean accept(File f) {
            if (f.getName().endsWith(".txt") || f.isDirectory()) return true;
            return false;
        }

        @Override
        public String getDescription() {
            return "text documents";
        }

    }
    
    @SuppressWarnings("serial")
	private class LATableModel extends AbstractTableModel {

        private int rowCount = 0;
        private int columnCount = 0;
        private List<ResString> data = new ArrayList<ResString>();

        LATableModel() {
        	
        }

        LATableModel(TreeSet<ResString> resultLA) {
            this.data.addAll(resultLA);
            rowCount = this.data.size();
            columnCount = 6;
        }

        public int getRowCount() {
            return rowCount;
        }

        public int getColumnCount() {
            return columnCount;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
                case 0: return rowIndex + 1;
                case 1: return data.get(rowIndex).getStrnum();
                case 2: return data.get(rowIndex).getColnum();
                case 3: return data.get(rowIndex).getStr();
                case 4: return data.get(rowIndex).getLex();
                case 5: return data.get(rowIndex).getCode();
                default: return null;
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public String getColumnName(int index) {
            switch (index) {
                case 0: return "#";
                case 1: return "Row";
                case 2: return "Col";
                case 3: return "Name";
                case 4: return "Type";
                case 5: return "Code";
                default: return null;
            }
        }

    }

    private class MyParseTableModel extends AbstractTableModel {

        private int rowCount;
        private int columnCount;
        private String[][] data;
        private String[] nonTerminals;
        private String[] terminals;

        MyParseTableModel(String[][] data, String[] nonTerminals, String[] terminals) {
            this.data = data;
            this.nonTerminals = nonTerminals;
            this.terminals = terminals;
            rowCount = data.length;
            columnCount = data[0].length + 1;
        }

        public int getRowCount() {
            return rowCount;
        }

        public int getColumnCount() {
            return columnCount;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0) return nonTerminals[rowIndex];
            else return data[rowIndex][columnIndex - 1];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            if (columnIndex == 0) return false;
            else return true;
        }

        @Override
        public String getColumnName(int index) {
            if (index == 0) return "";
            else return terminals[index - 1];
        }

    }

    private class SymbolTableModel extends AbstractTableModel {

        private int rowCount = 0;
        private int columnCount = 0;
        private ArrayList<Symbol> data1;
        private ArrayList<Block> data;

        SymbolTableModel(ArrayList<Block> blocks) {
//            data = symb;
        	data = blocks;
            rowCount = data.size();
            columnCount = 2;
        }

        public int getRowCount() {
            return rowCount;
        }

        public int getColumnCount() {
            return columnCount;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            String name;
            Block currentSymbol = data.get(rowIndex);
//
//            if (currentSymbol.getType() == Symbol.CONSTRUCTOR ||
//                currentSymbol.getType() == Symbol.DESTRUCTOR) {
//                name = "-";
//            } else if (currentSymbol.getCurClass() != null &&
//                       currentSymbol.getLevel().length == 1 &&
//                       currentSymbol.getType() != Symbol.CLASS) {
//                name = currentSymbol.getCurClass().getWordName() + "." + currentSymbol.getName();
//            } else {
//                name = currentSymbol.getName();
//            }
            switch (columnIndex) {
                case 0: return rowIndex + 1;
                case 1: return currentSymbol.toString();
                default: return null;
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public String getColumnName(int index) {
            switch (index) {
                case 0: return "№";
                case 1: return "�?азва";
                case 2: return "Тип";
                case 3: return "Блок";
                case 4: return "Параметри";
                default: return null;
            }
        }

    }
//
//
//    private class TetradsTableModel extends AbstractTableModel {
//
//        private int rowCount = 0;
//        private int columnCount = 0;
//        private ArrayList<Tetrad> data;
//
//        TetradsTableModel() {
//            data = new ArrayList<Tetrad>();
//            rowCount = 1;
//            columnCount = 5;
//        }
//
//        public int getRowCount() {
//            return rowCount;
//        }
//
//        public int getColumnCount() {
//            return columnCount;
//        }
//
//        public Object getValueAt(int rowIndex, int columnIndex) {
//            switch (columnIndex) {
//                case 0: return rowIndex;
//                case 1: return data.get(rowIndex).getOperator();
//                case 2: return data.get(rowIndex).getArg1();
//                case 3: return data.get(rowIndex).getArg2();
//                case 4: return data.get(rowIndex).getResult();
//                default: return null;
//            }
//        }
//
//        @Override
//        public boolean isCellEditable(int rowIndex, int columnIndex) {
//            return false;
//        }
//
//        @Override
//        public String getColumnName(int index) {
//            switch (index) {
//                case 0: return "№";
//                case 1: return "Oper";
//                case 2: return "Arg1";
//                case 3: return "Arg2";
//                case 4: return "Result";
//                default: return null;
//            }
//        }
//
//    }

    class ColorColumnRenderer extends DefaultTableCellRenderer {
        Color bkgndColor, fgndColor;

        public ColorColumnRenderer(Color bkgnd, Color foregnd) {
            super();
            bkgndColor = bkgnd;
            fgndColor = foregnd;
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                 boolean hasFocus, int row, int column) {
            Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            cell.setBackground( bkgndColor );
            cell.setForeground( fgndColor );
            return cell;
        }

    }


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }




    

    
    ArrayList<Word> container;


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTableLA;
    private javax.swing.JTable jTableTI;
    private javax.swing.JTable jTableTSA;
    private javax.swing.JTable jTableTriads;
    private javax.swing.JTextArea jTextAreaCode;
    private javax.swing.JTextArea jTextAreaGrammar;
    private javax.swing.JTextArea jTextAreaST;
    private javax.swing.JTextField jTextFieldCaretPosition;
    // End of variables declaration//GEN-END:variables

}
